<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends CommonController {
    public function index(){
    	$data=M('admin')->select();
		$this->assign('data',$data);	
    	$this->display('admin');
    }
	//管理员添加
	public function add(){
		if(IS_POST){
			if($_POST['passwdF']=='') $this->error('密码不能为空！');
			if(strlen($_POST['passwdF'])<6) $this->error('密码不能小于6位！');
			if($_POST['passwdF']!=$_POST['passwdS']) $this->error('两次输入的密码不相同！');
			//post数据
			$data=array(
				'username'=>I('post.username'),
				'password'=>md5(I('post.passwdF'))
			);
			//判断管理员是否存在
			$s=M('admin')->where(array('username'=>$data['username']))->find();
			if(!$s){
				$status=M('admin')->add($data);
				if($status){
					$this->success('添加成功');
				}else{
					$this->error('添加失败');
				}
			}else{
				$this->error('管理员已经存在');
			}
			
		}else{
			$this->display('add_admin');
		}
		
	}
	//密码修改
	public function password(){
		if(IS_POST){
			if($_POST['passwdF']=='') $this->error('密码不能为空！');
			if(strlen($_POST['passwdF'])<6) $this->error('密码不能小于6位！');
			if($_POST['passwdF']!=$_POST['passwdS']) $this->error('两次输入的密码不相同！');
			$password=md5(I('post.passwdF'));
			$adid=I('post.adid','','intval');
			$status=M('admin')->where(array('adid'=>$adid))->save(array('password'=>$password));
			if($status){
				$this->success('密码修改成功！请退出重新登录！');
			}else{
				$this->error('密码修改失败！');
			}
		}else{
			$this->display();	
		}
		
	}
	//锁定
	public function lock(){
		$adid=I('get.adid','','intval');
		$status=M('admin')->where(array('adid'=>$adid))->save(array('lock'=>1));
		if($status){
			$this->success('锁定成功');
		}else{
			$this->success('锁定失败');
		}
	}
	//解锁
	public function unlock(){
		$adid=I('get.adid','','intval');
		$status=M('admin')->where(array('adid'=>$adid))->save(array('lock'=>0));
		if($status){
			$this->success('解锁成功');
		}else{
			$this->success('解锁失败');
		}
	}
	//删除
	public function del(){
		$adid=I('get.adid','','intval');
		$status=M('admin')->where(array('adid'=>$adid))->delete();
		if($status){
			$this->success('删除成功');
		}else{
			$this->success('删除失败');
		}
	}
}